<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <el-form-item label="售后问题" prop="problem">
        <el-input
          v-model="queryParams.problem"
          placeholder="请输入售后问题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="受理结果" prop="isAccep">
        <el-select v-model="queryParams.isAccep" placeholder="请选择受理类型">
          <el-option
            v-for="item in [
              { value: '1', label: '待处理' },
              { value: '2', label: '受理中' },
              { value: '3', label: '已完成' },
            ]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="受理工作人员" prop="accepStaff">
        <el-input v-model="queryParams.accepStaff" placeholder="请输入受理工作人员" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="受理方案" prop="accepPlan">
        <el-select v-model="queryParams.accepPlan" placeholder="请选择受理方案">
          <el-option
            v-for="item in [
              { value: '1', label: '全额退款' },
              { value: '2', label: '部分退款' },
              { value: '3', label: '补偿券' },
            ]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="退款金额" prop="refundAmount">
        <el-input
          v-model="queryParams.refundAmount"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="受理时间" prop="accepTime">
        <el-date-picker
          clearable
          v-model="queryParams.accepTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择受理时间"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="优惠券ID" prop="coupId">
        <el-input v-model="queryParams.coupId" placeholder="请输入优惠券ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:aftersales:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:aftersales:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:aftersales:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:aftersales:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="aftersalesList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单ID" align="center" prop="id" />
      <el-table-column label="售后问题" align="center" prop="problem" />
      <el-table-column label="问题描述" align="center" prop="description">
        <template slot-scope="scope">
          <el-popover
            placement="bottom"
            title="问题描述"
            width="200"
            trigger="click"
            :content="scope.row.description"
          >
            <p class="desc_p" slot="reference">{{ scope.row.description }}</p>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="场景图片" align="center" prop="images">
        <template slot-scope="scope">
          <image-preview
            :src="api_host + scope.row.images"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="受理结果" align="center" prop="isAccep">
        <template slot-scope="scope">
          <el-tag type="danger" size="mini" v-if="scope.row.isAccep == 1"
            >待处理</el-tag
          >
          <el-tag type="warning" size="mini" v-if="scope.row.isAccep == 2"
            >受理中</el-tag
          >
          <el-tag type="success" size="mini" v-if="scope.row.isAccep == 3"
            >已完成</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="受理工作人员" align="center" prop="accepStaff" />
      <el-table-column label="受理方案" align="center" prop="accepPlan">
        <template slot-scope="scope">
          <el-tag type="danger" size="mini" v-if="scope.row.accepPlan == 1"
            >全额退款</el-tag
          >
          <el-tag type="warning" size="mini" v-if="scope.row.accepPlan == 2"
            >部分退款</el-tag
          >
          <el-tag type="success" size="mini" v-if="scope.row.accepPlan == 3"
            >补偿券</el-tag
          >
          <el-tag type="info" size="mini" v-if="scope.row.accepPlan == 4"
            >拒绝退款</el-tag
          >
        </template>
      </el-table-column>
      <!-- <el-table-column label="退款金额" align="center" prop="refundAmount" /> -->
      <el-table-column
        label="受理时间"
        align="center"
        prop="accepTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.accepTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="优惠券ID" align="center" prop="coupId" /> -->
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:aftersales:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:aftersales:remove']"
          >删除</el-button> -->
          <el-button
            type="primary"
            size="mini"
            @click="handleShouhou(scope.row)"
            >售后处理</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单售后管理对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="售后问题" prop="problem">
          <el-input v-model="form.problem" placeholder="请输入售后问题" />
        </el-form-item>
        <el-form-item label="问题描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="场景图片" prop="images">
          <el-input
            v-model="form.images"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          label="是否受理  1待处理  2受理中  3已完成"
          prop="isAccep"
        >
          <el-input
            v-model="form.isAccep"
            placeholder="请输入是否受理  1待处理  2受理中  3已完成"
          />
        </el-form-item>
        <el-form-item label="受理工作人员" prop="accepStaff">
          <el-input
            v-model="form.accepStaff"
            placeholder="请输入受理工作人员"
          />
        </el-form-item>
        <el-form-item
          label="受理方案 1全额退款 2部分退款 3补偿券"
          prop="accepPlan"
        >
          <el-input
            v-model="form.accepPlan"
            placeholder="请输入受理方案 1全额退款 2部分退款 3补偿券"
          />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="受理时间" prop="accepTime">
          <el-date-picker
            clearable
            v-model="form.accepTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择受理时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="优惠券ID" prop="coupId">
          <el-input v-model="form.coupId" placeholder="请输入优惠券ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->

    <el-dialog title="处理售后" :visible.sync="dialogFormVisible">
      <el-steps
        :space="200"
        :active="aftersales.isAccep - 1"
        finish-status="success"
      >
        <el-step title="售后待处理"></el-step>
        <el-step title="售后受理中"></el-step>
        <el-step
          v-if="aftersales.isAccep == 3"
          title="售后已完成"
          status="success"
        ></el-step>
        <el-step v-else title="售后已完成"></el-step>
      </el-steps>
      <!-- <div style="display: flex; justify-content: space-between" class='mt10'> -->
      <el-card class="mt10">
        <div slot="header" class="clearfix">
          <span>订单信息</span>
        </div>
        <div class="item">
          <span>订单号:{{ form2.orderCode }}</span>
        </div>
        <div class="item">
          <span>创建时间:{{ form2.createTime }}</span>
        </div>
        <div class="item">
          <span>支付时间:{{ form2.payTime }}</span>
        </div>
        <div class="item">
          <span>订单金额:{{ form2.orderPrice }}</span>
        </div>
      </el-card>
      <el-card class="mt10">
        <div slot="header" class="clearfix">
          <span>商品信息</span>
        </div>
        <div class="item">
          <span>商品类型:{{ orderPro.proName }}</span>
        </div>
        <div class="mt10">
          <image-preview
            :src="api_host + orderPro.icon"
            :width="100"
            :height="100"
          />
        </div>
        <div v-for="(item, index) in orderDetails" :key="index">
          <p>商品规格:{{ item.name }}</p>
          <p>商品价格:{{ item.price }}</p>
          <p>商品数量:{{ item.buyCount }}</p>
        </div>
      </el-card>
      <!-- </div> -->
      <div class="mt10" v-if="orderAppend != null">
        <el-card>
          <div slot="header" class="clearfix">
            <span>追加服务</span>
          </div>
          <div class="mt10">
            <span>追加金额:￥{{ orderAppend.price }}</span>
          </div>
          <!-- <div class="mt10">
            <span>追加内容：</span>
          </div>
          <div class="mt10">
            <span>{{ orderAppend.describe }}</span>
          </div>
          <div class="mt10" v-if="form.sceneImages">
            <image-preview
              v-for="(item, index) in form.sceneImages.split(',')"
              :src="api_host + item"
              :width="100"
              :height="100"
            />
          </div> -->
        </el-card>
      </div>
      <div v-else>
        <el-card style="margin-top: 20px">
          <div slot="header" class="clearfix">
            <span>追加服务</span>
          </div>
          <div>
            <span>无追加服务</span>
          </div>
        </el-card>
      </div>
      <el-card class="mt10">
        <div slot="header" class="clearfix">
          <span>售后信息</span>
        </div>
        <!-- <div class="item">
          <span>申请售后时间:{{ form.createTime }}</span>
        </div> -->
        <div class="item">
          <span v-if="form.isAccep == 1"
            >售后状态：<el-tag>待处理</el-tag></span
          >
          <span v-if="form.isAccep == 2"
            >售后状态：<el-tag>受理中</el-tag></span
          >
          <span v-if="form.isAccep == 3"
            >售后状态：<el-tag>已处理</el-tag></span
          >
        </div>
        <div class="item">
          <span>售后问题:{{ form.problem }}</span>
        </div>
        <div class="item">
          <span>问题描述:{{ form.description }}</span>
        </div>
        <div class="mt10">
          <image-preview :src="form.images" :width="100" :height="100" />
        </div>
      </el-card>
      <el-card class="mt10" v-if="coupon!=null">
        <div slot="header" class="clearfix">
          <span>优惠券信息</span>
        </div>
        <div class="item">
          <span>优惠券名称:{{ coupon.name }}</span>
        </div>
        <div class="item">
          <span>优惠券类型:{{ coupon.coupTitle }}</span>
        </div>
        <div class="mt10">
          <image-preview :src="api_host+coupon.icon" :width="100" :height="100" />
        </div>
      </el-card>
      <el-form ref="form" :model="form" :rules="rules" label-position="top">
        <el-form-item label="处理结果">
          <el-radio-group
            v-model="form.accepPlan"
            :disabled="form.isAccep == 3"
          >
            <el-radio label="1">全额退款</el-radio>
            <el-radio label="2">部分退款</el-radio>
            <el-radio label="3">优惠券补偿</el-radio>
            <el-radio label="4">拒绝退款</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="退款金额" v-if="form.accepPlan == 2">
          <p>
            <el-input
              v-model="form.refundAmount"
              placeholder="请输入退款金额"
              style="width: 200px"
              :disabled="form.isAccep == 3"
            ></el-input>
            元
          </p>
        </el-form-item>
        <el-form-item label="优惠券补偿" v-if="form.accepPlan == 3">
          <el-select
            v-model="form.coupId"
            placeholder="请选择补偿优惠券"
            :disabled="form.isAccep == 3"
          >
            <el-option
              v-for="item in couponList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回复">
          <el-input
            type="textarea"
            v-model="form.accepDesc"
            placeholder="请输入回复内容"
            style="width: 400px"
            :disabled="form.isAccep == 3"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="form.isAccep != 3">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm2">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCoupon,
  listCoupon2,
  getCoupon,
  delCoupon,
  addCoupon,
  updateCoupon,
} from "@/api/system/coupon";
import {
  listAftersales,
  getAftersales,
  delAftersales,
  addAftersales,
  updateAftersales,
} from "@/api/system/aftersales";
import { getOrder } from "@/api/system/order";

export default {
  name: "Aftersales",
  data() {
    return {
      c_id: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单售后管理表格数据
      aftersalesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        problem: null,
        description: null,
        images: null,
        isAccep: null,
        accepStaff: null,
        accepPlan: null,
        refundAmount: null,
        accepTime: null,
        coupId: null,
        accepDesc: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dialogFormVisible: false,
      // 优惠券表格数据
      couponList: [],
      options: [],
      activatedFlag: true,
      form2: {},
      orderPro: {},
      orderDetails: [],
      aftersales: {},
      orderAppend:{},
      coupon:{}
    };
  },
  created() {
    this.getList();
    this.getList2();
    // console.log(this.$route.fullPath);
  },
  // warch:{
  //   '$route': {
  //     handler () {
  //       this.getList2()
  //     },
  //     immediate: true,
  //   }
  // },
  activated() {
    this.activatedFlag = true;
    if (this.$route.fullPath === "/order/aftersales" && this.activatedFlag) {
      this.getList2();
    }
  },
  deactivated() {
    this.activatedFlag = false;
    // 清空保存listPageApi接口返回数据的容器
    // this.saveProductData = [];
  },
  methods: {
    /** 查询订单售后管理列表 */
    getList() {
      this.loading = true;
      listAftersales(this.queryParams).then((response) => {
        this.aftersalesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        problem: null,
        description: null,
        images: null,
        isAccep: null,
        accepStaff: null,
        accepPlan: null,
        refundAmount: null,
        accepTime: null,
        coupId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单售后管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAftersales(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单售后管理";
      });
    },
    /** 提交按钮 */
    submitForm2() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAftersales(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.dialogFormVisible = false;
              this.getList();
            });
          } else {
            addAftersales(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.dialogFormVisible = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除订单售后管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAftersales(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/aftersales/export",
        {
          ...this.queryParams,
        },
        `aftersales_${new Date().getTime()}.xlsx`
      );
    },
    /** 查询优惠券列表 */
    getList2() {
      this.loading = true;
      listCoupon2(this.queryParams).then((response) => {
        this.couponList = response.rows;
        // this.options = response.rows.map(item=>{
        //   let text = ''
        //   switch (item.conpType) {
        //     case 1:
        //       text = '满减券'
        //       break;
        //     case 2:
        //       text = '折扣券'
        //       break;
        //     case 3:
        //       text = '体验券'
        //       break;
        //     default:
        //       break;
        //   }
        //   return {
        //     value:item.conpType,
        //     label:text,
        //     children:[]
        //   }
        // })
        this.loading = false;
      });
    },
    //售后处理
    handleShouhou(item) {
      this.dialogFormVisible = true;
      this.form = item;
      // this.aftersales = item.aftersales;
      if (item.accepPlan != null) {
        this.form.accepPlan = item.accepPlan.toString();
      }

      const id = item.id;
      getOrder(id).then((response) => {
        this.form2 = response.data;
        this.orderPro = response.data.orderPro;
        this.orderDetails = response.data.orderDetails;
        this.aftersales = response.data.aftersales;
        this.coupon = response.data.coupon;
        this.orderAppend = response.data.orderAppend
      });
    },
  },
};
</script>

<style>
.desc_p {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.item {
  margin-bottom: 18px;
}
.box-card {
  width: 450px;
}
</style>
